package com.ball.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ball.dao.UserMapper;
import com.ball.entity.User;
import com.ball.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public String add(JSONObject json) {
//        int id=Integer.parseInt(json.getString("id"));
        String username=null;
        String password=null;
        String mail=null;
        String walletName=null;
        String walletAddress=null;
        String money=null;
        username = json.getString("username");
        password=json.getString("password");
        mail=json.getString("mail");
        walletName=json.getString("walletName");
        walletAddress=json.getString("walletAddress");
        money=json.getString("money");
        User user=new User(0,username,password,mail,walletName,walletAddress,money);
        userMapper.add(user);
        return "add user: "+user.toString();
    }

    @Override
    public String update(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        String username = json.getString("username");
        String password=json.getString("password");
        String mail=json.getString("mail");
        String walletName=json.getString("walletName");
        String walletAddress=json.getString("walletAddress");
        String money=json.getString("money");
        User user=new User(id,username,password,mail,walletName,walletAddress,money);
        userMapper.update(user);
        return "update user: "+user.toString();
    }
    @Override
    public String update(User user) {
        userMapper.update(user);
        return "update user: "+user.toString();
    }

    @Override
    public String deleteUserById(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        userMapper.deleteUserById(id);
        return "deleteUserById: "+id;
    }

    @Override
    public String deleteUserByName(JSONObject json) {
        String username = json.getString("username");
        userMapper.deleteUserByName(username);
        return "deleteUserById: "+username;
    }

    @Override
    public User queryUserById(JSONObject json) {
        int id=Integer.parseInt(json.getString("id"));
        return userMapper.queryUserById(id);
    }
    @Override
    public User queryUserByUsername(JSONObject json){
        String username = json.getString("username");
        return userMapper.queryUserByUsername(username);
    }
    @Override
    public List<User> queryUserList(JSONObject json) {
        return userMapper.queryUserList();
    }
}
